<template>
  <el-autocomplete
    v-model.trim="form"
    :fetch-suggestions="querySearchAsync"
    v-bind="$attrs"
    :debounce="800"
    @select="handleSelect"
  ></el-autocomplete>
</template>

<script>
import vModelMixins from "../../mixins/vModelMixins";

export default {
  name: "autoComplete",
  mixins: [vModelMixins],
  props: {
    type: {
      type: String
    },
    querys: {
      type: Object
    }
  },
  data() {
    return {};
  },
  methods: {
    queryMethod() {
      let ret = () => {};
      switch (this.type) {
        case "model":
          ret = this.$Api.Goods.nameOrModelOrRuleByLike({
            data: {
              model: this.form,
              ...this.querys
            }
          });
          break;
        case "name":
          ret = this.$Api.Goods.nameOrModelOrRuleByLike({
            data: {
              name: this.form,
              ...this.querys
            }
          });
          break;
        case "rule":
          ret = this.$Api.Goods.nameOrModelOrRuleByLike({
            data: {
              rule: this.form,
              ...this.querys
            }
          });
          break;

        default:
          break;
      }
      return ret;
    },
    async querySearchAsync(queryString, cb) {
      await this.$nextTick();
      let results = await this.queryMethod();
      results = Array.isArray(results) ? results : [];
      results = results.map(i => ({ value: i }));
      cb(results);
    },
    handleSelect(item) {
      // console.log(item);
    }
  }
};
</script>
